package mk.edu.ii.navigatemacedonia;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;


public class Subcategorys {
	ArrayList<Subcategory> subcategorys = new ArrayList<Subcategory>();
	
	public Subcategorys(Context cr){
		populate(cr);
	}
	
	public List<CharSequence> getSubcategoryList(Context context, int category){
		List<CharSequence> list = new ArrayList<CharSequence>();
		list.add(context.getResources().getString(R.string.all));
		
		for(int i=0; i<subcategorys.size(); i++){
			if(subcategorys.get(i).getCategory() == category)
				list.add(subcategorys.get(i).getSubcategory());
		}
		list.add(context.getResources().getString(R.string.other));
		return list;
	}
	
	public void populate(Context context){
		//getting reference to ContentResolver(used for manage the ContentProvider)
    	//ContentResolver cr = getContentResolver();
		

	    
    	// Return all the saved locations
    	Cursor c = context.getContentResolver().query(NMContentProvider.CONTENT_URI,
    	null, null, null, null);
    	//if there is any location in ContentProvider go through all of them
    	
    	
		if (c!=null && c.moveToFirst()) {
			subcategorys.clear();
			String subcategory = null;
			do {
				subcategory = c.getString(c
						.getColumnIndex(NMContentProvider.KEY_SUBCATEGORY));
				
				// Extract the subcategorys
				subcategorys.add(new Subcategory(c.getInt(c.getColumnIndex(NMContentProvider.KEY_ID)), subcategory, c.getInt(c
						.getColumnIndex(NMContentProvider.KEY_CATEGORY))));

			} while (c.moveToNext());
		}
		c.close();
	}
	
	public int getSubcategroyId(String subcategory){
		for(int i=0; i< subcategorys.size();i++){
			if(subcategorys.get(i).getSubcategory().equals(subcategory)){
				return subcategorys.get(i).getId();
			}
		}
		return 0;
	}
}
class Subcategory{
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getSubcategory() {
		return subcategory;
	}

	public void setSubcategory(String subcategory) {
		this.subcategory = subcategory;
	}

	public int getCategory() {
		return category;
	}

	public void setCategory(int category) {
		this.category = category;
	}

	private String subcategory;
	private int category;
	private int id;
	
	public Subcategory(int id, String subcategory, int category){
		this.subcategory = subcategory;
		this.category = category;
		this.id = id;
	}
}
